/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import dao.LeiDAO;
import dao.ProfDAO;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import Bean.Escola;
import Bean.Lei;
import Bean.Professor;
import Bean.Turno;
import javax.ejb.EJB;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import util.JPAUtil;

/**
 *
 * @author alexandre
 */
@ManagedBean
@SessionScoped
public class LeiFaces implements Serializable{
    
    private List<Lei> leis;
    private Lei selectedLEi;
    private Professor selectedProfessor;
    private Escola selectedEscola;
    private Turno selectedTurno;
    @EJB
    private LeiDAO leiDAO;
    private List<Professor> professores;
    @EJB
    private ProfDAO profDAO;

    public Turno getSelectedTurno() {
        return selectedTurno;
    }

    public void setSelectedTurno(Turno selectedTurno) {
        this.selectedTurno = selectedTurno;
    }

    
    public List<Professor> getProfessores() {
        return professores;
    }

    public void setProfessores(List<Professor> professores) {
        this.professores = professores;
    }

   
    
     public void reset(){
         
     RequestContext.getCurrentInstance().reset("lei:pnlLotacao");
    selectedLEi = new Lei();
     }
    
    
    public String startLei(){
        selectedLEi = new Lei();
        selectedEscola = new Escola();
        selectedTurno = new Turno();
        selectedProfessor = new Professor();
        return "/admin/novo_lei.jsf";
    }
    
     public String startEditLei(){
        
        return "/admin/editLEI.jsf";
    }
    
    public void add(){
       
     
        selectedLEi.setEscola(selectedEscola);
         selectedLEi.setProf(selectedProfessor);
        selectedLEi.setTurno(selectedTurno);
        leiDAO.add(selectedLEi);
        //ZERA OBJETOS
        selectedLEi = new Lei();
        selectedEscola = new Escola();
        selectedTurno = new Turno();
        selectedProfessor = new Professor();
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "SIAC", "Dados Gravados com Sucesso!!");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
    }
    
    public void del(){
        leiDAO.del(selectedLEi);
        FacesMessage message  = new FacesMessage("Dados Apagados com Sucesso!!");
        FacesContext.getCurrentInstance().addMessage(null, message);
        selectedLEi = new Lei();
    }
    public void edit(){
        leiDAO.edit(selectedLEi);
         FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "SIAC", "Dados Editados com Sucesso!!");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
        selectedLEi = new Lei();
        
    }
    
    
    public Professor getSelectedProfessor() {
        return selectedProfessor;
    }

    public void setSelectedProfessor(Professor selectedProfessor) {
        this.selectedProfessor = selectedProfessor;
    }

    public Escola getSelectedEscola() {
        return selectedEscola;
    }

    public void setSelectedEscola(Escola selectedEscola) {
        this.selectedEscola = selectedEscola;
    }
    

    
    
    public List<Lei> getLeis() {
        return leis;
    }

    public void setLeis(List<Lei> leis) {
        this.leis = leis;
    }

    public Lei getSelectedLEi() {
        return selectedLEi;
    }

    public void setSelectedLEi(Lei selectedLEi) {
        this.selectedLEi = selectedLEi;
    }
    
    public List<Lei> getAllLeis(){
       leis = leiDAO.AllLeis();
       return leis;
        
        // return JPAUtil.getInstance().getList(Lei.class,"SELECT lei FROM Lei lei");
    }
    
    public void profPorEscola(){
       professores = null;
       professores = profDAO.getRetornaProfPorEscola(selectedEscola.getID());
      
    }
    
     public void onEdit(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Dados Editados!!", ((Lei) event.getObject()).getEscola().getEscola());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
         public void onCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Edição Cancelada!!", ((Lei) event.getObject()).getEscola().getEscola());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
